package com.wbk.lotterysystemlotteryservice.controller.admin.biz;

import com.wbk.lottery.qo.LotteryAuditQO;
import com.wbk.lottery.vo.LotteryVO;
import com.wbk.lotterysystemlotteryservice.entity.Lottery;
import com.wbk.lotterysystemlotteryservice.service.LotteryService;
import com.wbk.system.feign.EmailFeign;
import com.wbk.system.qo.LotteryAuditResultQO;
import com.wbk.user.feign.UserFeign;
import com.wbk.user.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import result.Result;

@Component
public class LotteryAuditBiz {

    @Autowired
    LotteryService lotteryService;

    @Autowired
    UserFeign userFeign;

    @Autowired
    EmailFeign emailFeign;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Transactional(propagation = Propagation.REQUIRED)
    public Result<LotteryVO> lotteryAudit(LotteryAuditQO lotteryAuditQO) {
        Lottery lottery = lotteryService.getLotteryById(lotteryAuditQO.getId());
        if (lottery == null) {
            return new Result(100, "此抽奖活动不存在", null);
        }
        if (lottery.getAuditStatus() != 0) {
            return new Result<>(100, "此抽奖活动已审核完毕，无需重复操作", null);
        }
        if (lotteryAuditQO.getAuditStatus() != 1 && lotteryAuditQO.getAuditStatus() != -1) {
            return new Result<>(100, "参数异常", null);
        }
        Result<UserVO> user = userFeign.user(lottery.getStoreUserNo());
        if (user.getCode() == 100) {
            return new Result(100, "找不到用户信息", null);
        }

        LotteryVO lotteryVO = new LotteryVO();
        LotteryAuditResultQO lotteryAuditResultQO = new LotteryAuditResultQO();

        //审核不通过
        if (lotteryAuditQO.getAuditStatus() == -1) {
            //更新审核状态
            lottery.setAuditStatus((byte) -1);
            lottery.setAuditOpinion(lotteryAuditQO.getAuditOpinion());
            lotteryService.audit(lottery);

            //邮件通知
            lotteryAuditResultQO.setEmail(user.getData().getEmail()).setAuditStatus(lotteryAuditQO.getAuditStatus()).setAuditOpinion(lotteryAuditQO.getAuditOpinion());
            emailFeign.lotteryAuditResult(lotteryAuditResultQO);

            BeanUtils.copyProperties(lottery, lotteryVO);
            return new Result<>(200, "审核不通过，请重新修改相关信息", lotteryVO);
        }

        //审核通过
        //更新审核状态
        lottery.setAuditStatus((byte) 1);
        lottery.setAuditOpinion(lotteryAuditQO.getAuditOpinion());
        lotteryService.audit(lottery);

        //邮件通知
        lotteryAuditResultQO.setEmail(user.getData().getEmail()).setAuditStatus(lotteryAuditQO.getAuditStatus()).setAuditOpinion(lotteryAuditQO.getAuditOpinion());
        emailFeign.lotteryAuditResult(lotteryAuditResultQO);

        //缓存中生成key用于统计参与人数
        stringRedisTemplate.opsForValue().set("抽奖活动id-" + lottery.getId() + "的当前已参与人数:", "0");

        BeanUtils.copyProperties(lottery, lotteryVO);
        return new Result<>(200, "审核通过", lotteryVO);
    }
}
